package jdbc;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class adminchangecourse
 */
@WebServlet("/src/adminchangecourse")
public class adminchangecourse extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public adminchangecourse() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setHeader("Access-Control-Allow-Origin", "*");//模板
	    response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");//模板
	    response.setHeader("Access-Control-Allow-Headers", "Content-Type");//模板
	    //设置编码格式
	    request.setCharacterEncoding("UTF-8");//模板
	    response.setCharacterEncoding("UTF-8");//模板
		
	    String courseName = request.getParameter("courseName");
	    String coursemajor = request.getParameter("coursemajor");
	    String courseschool = request.getParameter("courseschool");
	    String courseTeacher = request.getParameter("courseTeacher");
	    String courseclassroom = request.getParameter("courseclassroom");
	    String coursetime = request.getParameter("coursetime");
	    String courseid = request.getParameter("courseid");

	    //int id = Integer.parseInt(student_id);
	    hello a = new hello();//模板
	    try {//模板
	    		response.getWriter().write("true");
				ResultSet b;
				a.doupdate("UPDATE course SET course_name = ? , course_major = ? , course_college = ? , course_classroom = ? , course_time = ? where course_id = ? ",courseName,coursemajor,courseschool,courseclassroom,coursetime,courseid);
			    
				b=a.doselect("SELECT teacher_id from teacher where teacher_name = ? ", courseTeacher);
			    while(b.next()) {
					a.doupdate("UPDATE course SET course_teacher = ? where course_id = ?",b.getString("teacher_id"),courseid );
			    }
				
				//response.getWriter().write(b.getString("course_name")+";");//这里表示的是往返回的那个response里面添加字符串，修改write括号后的内容即可
				//response.getWriter().write(b.getString("course_major")+";");
				//response.getWriter().write(b.getString("course_id")+";");
				
				//response.getWriter().write(b.getString("selected")+";");
				//模板
				b.close();
		} catch (SQLException e) {//模板
			e.printStackTrace();//模板
		}//模板
	    }

}
